Skills Network Logo

Extracting and Visualizing Stock Data

Description

Extracting essential data from a dataset and displaying it is a necessary part of data science; therefore individuals can make correct decisions based on the data. In this assignment, you will extract some stock data, you will then display this data in a graph.

Table of Contents

  • Define a Function that Makes a Graph
  • Question 1: Use yfinance to Extract Stock Data
  • Question 2: Use Webscraping to Extract Tesla Revenue Data
  • Question 3: Use yfinance to Extract Stock Data
  • Question 4: Use Webscraping to Extract GME Revenue Data
  • Question 5: Plot Tesla Stock Graph
  • Question 6: Plot GameStop Stock Graph

Estimated Time Needed: 30 min


Note:- If you are working Locally using anaconda, please uncomment the following code and execute it. Use the version as per your python version.

In [1]:
!pip install yfinance
!pip install bs4
!pip install nbformat
Requirement already satisfied: yfinance in /opt/conda/lib/python3.11/site-packages (0.2.50)
Requirement already satisfied: pandas>=1.3.0 in /opt/conda/lib/python3.11/site-packages (from yfinance) (2.2.3)
Requirement already satisfied: numpy>=1.16.5 in /opt/conda/lib/python3.11/site-packages (from yfinance) (2.1.3)
Requirement already satisfied: requests>=2.31 in /opt/conda/lib/python3.11/site-packages (from yfinance) (2.31.0)
Requirement already satisfied: multitasking>=0.0.7 in /opt/conda/lib/python3.11/site-packages (from yfinance) (0.0.11)
Requirement already satisfied: lxml>=4.9.1 in /opt/conda/lib/python3.11/site-packages (from yfinance) (5.3.0)
Requirement already satisfied: platformdirs>=2.0.0 in /opt/conda/lib/python3.11/site-packages (from yfinance) (4.2.1)
Requirement already satisfied: pytz>=2022.5 in /opt/conda/lib/python3.11/site-packages (from yfinance) (2024.1)
Requirement already satisfied: frozendict>=2.3.4 in /opt/conda/lib/python3.11/site-packages (from yfinance) (2.4.6)
Requirement already satisfied: peewee>=3.16.2 in /opt/conda/lib/python3.11/site-packages (from yfinance) (3.17.8)
Requirement already satisfied: beautifulsoup4>=4.11.1 in /opt/conda/lib/python3.11/site-packages (from yfinance) (4.12.3)
Requirement already satisfied: html5lib>=1.1 in /opt/conda/lib/python3.11/site-packages (from yfinance) (1.1)
Requirement already satisfied: soupsieve>1.2 in /opt/conda/lib/python3.11/site-packages (from beautifulsoup4>=4.11.1->yfinance) (2.5)
Requirement already satisfied: six>=1.9 in /opt/conda/lib/python3.11/site-packages (from html5lib>=1.1->yfinance) (1.16.0)
Requirement already satisfied: webencodings in /opt/conda/lib/python3.11/site-packages (from html5lib>=1.1->yfinance) (0.5.1)
Requirement already satisfied: python-dateutil>=2.8.2 in /opt/conda/lib/python3.11/site-packages (from pandas>=1.3.0->yfinance) (2.9.0)
Requirement already satisfied: tzdata>=2022.7 in /opt/conda/lib/python3.11/site-packages (from pandas>=1.3.0->yfinance) (2024.2)
Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.11/site-packages (from requests>=2.31->yfinance) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.11/site-packages (from requests>=2.31->yfinance) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.11/site-packages (from requests>=2.31->yfinance) (2.2.1)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.11/site-packages (from requests>=2.31->yfinance) (2024.8.30)
Requirement already satisfied: bs4 in /opt/conda/lib/python3.11/site-packages (0.0.2)
Requirement already satisfied: beautifulsoup4 in /opt/conda/lib/python3.11/site-packages (from bs4) (4.12.3)
Requirement already satisfied: soupsieve>1.2 in /opt/conda/lib/python3.11/site-packages (from beautifulsoup4->bs4) (2.5)
Requirement already satisfied: nbformat in /opt/conda/lib/python3.11/site-packages (5.10.4)
Requirement already satisfied: fastjsonschema>=2.15 in /opt/conda/lib/python3.11/site-packages (from nbformat) (2.19.1)
Requirement already satisfied: jsonschema>=2.6 in /opt/conda/lib/python3.11/site-packages (from nbformat) (4.22.0)
Requirement already satisfied: jupyter-core!=5.0.*,>=4.12 in /opt/conda/lib/python3.11/site-packages (from nbformat) (5.7.2)
Requirement already satisfied: traitlets>=5.1 in /opt/conda/lib/python3.11/site-packages (from nbformat) (5.14.3)
Requirement already satisfied: attrs>=22.2.0 in /opt/conda/lib/python3.11/site-packages (from jsonschema>=2.6->nbformat) (23.2.0)
Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /opt/conda/lib/python3.11/site-packages (from jsonschema>=2.6->nbformat) (2023.12.1)
Requirement already satisfied: referencing>=0.28.4 in /opt/conda/lib/python3.11/site-packages (from jsonschema>=2.6->nbformat) (0.35.1)
Requirement already satisfied: rpds-py>=0.7.1 in /opt/conda/lib/python3.11/site-packages (from jsonschema>=2.6->nbformat) (0.18.0)
Requirement already satisfied: platformdirs>=2.5 in /opt/conda/lib/python3.11/site-packages (from jupyter-core!=5.0.*,>=4.12->nbformat) (4.2.1)
In [2]:
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots

In Python, you can ignore warnings using the warnings module. You can use the filterwarnings function to filter or ignore specific warning messages or categories.

In [3]:
import warnings
# Ignore all warnings
warnings.filterwarnings("ignore", category=FutureWarning)

Define Graphing Function¶

In this section, we define the function make_graph. You don't have to know how the function works, you should only care about the inputs. It takes a dataframe with stock data (dataframe must contain Date and Close columns), a dataframe with revenue data (dataframe must contain Date and Revenue columns), and the name of the stock.

In [4]:
def make_graph(stock_data, revenue_data, stock):
    fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
    stock_data_specific = stock_data[stock_data.Date <= '2021-06-14']
    revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
    fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
    fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
    fig.update_xaxes(title_text="Date", row=1, col=1)
    fig.update_xaxes(title_text="Date", row=2, col=1)
    fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
    fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
    fig.update_layout(showlegend=False,
    height=900,
    title=stock,
    xaxis_rangeslider_visible=True)
    fig.show()

Use the make_graph function that we’ve already defined. You’ll need to invoke it in questions 5 and 6 to display the graphs and create the dashboard.

Note: You don’t need to redefine the function for plotting graphs anywhere else in this notebook; just use the existing function.

Question 1: Use yfinance to Extract Stock Data¶

Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is Tesla and its ticker symbol is TSLA.

In [6]:
import yfinance as yf

ticker_symbol = "TSLA"
tesla_ticker = yf.Ticker(ticker_symbol)

Using the ticker object and the function history extract stock information and save it in a dataframe named tesla_data. Set the period parameter to "max" so we get information for the maximum amount of time.

In [7]:
tesla_data = tesla_ticker.history(period="max")

Reset the index using the reset_index(inplace=True) function on the tesla_data DataFrame and display the first five rows of the tesla_data dataframe using the head function. Take a screenshot of the results and code from the beginning of Question 1 to the results below.

In [10]:
tesla_data.reset_index(inplace=True)

tesla_data.head()
Out[10]:
index Date Open High Low Close Volume Dividends Stock Splits
0 0 2010-06-29 00:00:00-04:00 1.266667 1.666667 1.169333 1.592667 281494500 0.0 0.0
1 1 2010-06-30 00:00:00-04:00 1.719333 2.028000 1.553333 1.588667 257806500 0.0 0.0
2 2 2010-07-01 00:00:00-04:00 1.666667 1.728000 1.351333 1.464000 123282000 0.0 0.0
3 3 2010-07-02 00:00:00-04:00 1.533333 1.540000 1.247333 1.280000 77097000 0.0 0.0
4 4 2010-07-06 00:00:00-04:00 1.333333 1.333333 1.055333 1.074000 103003500 0.0 0.0

Question 2: Use Webscraping to Extract Tesla Revenue Data¶

Use the requests library to download the webpage https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm Save the text of the response as a variable named html_data.

In [11]:
url= "https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue"
html_data=requests.get(url).text

Parse the html data using beautiful_soup using parser i.e html5lib or html.parser.

In [17]:
soup = BeautifulSoup(html_data,"html5lib")
print(soup.prettify())
<html>
 <head>
 </head>
 <body>
  <div style="margin: 50px auto; width: 50%; border: 1px solid #dfdfdf; padding: 20px 50px 30px 50px; font-family:helvetica;">
   <h1>
    We do not allow automated access to our servers.
   </h1>
   <h2>
    <p>
     Automated access to our data is prohibited by our data provider.
    </p>
    <p>
     If you are a user attempting to access the site via a browser, please follow this process to regain access:
    </p>
    <ul>
     <li>
      Go to
      <a href="https://whatismyipaddress.com/" rel="noopener noreferrer" target="_blank">
       whatismyipaddress
      </a>
      and obtain your IPv4 address
     </li>
     <li>
      Email us your IPv4 address at
      <a class="__cf_email__" data-cfemail="4821262e270825292b3a273c3a2d262c3b66262d3c" href="/cdn-cgi/l/email-protection">
       [email protected]
      </a>
     </li>
     <li>
      We will add you to our whitelist within 24 hours
     </li>
    </ul>
   </h2>
  </div>
  <script data-cfasync="false">
   !function(){"use strict";function e(e){try{if("undefined"==typeof console)return;"error"in console?console.error(e):console.log(e)}catch(e){}}function t(e,t){var r=e.substr(t,2);return parseInt(r,16)}function r(r,n){for(var c="",o=t(r,n),a=n+2;a<r.length;a+=2){var l=t(r,a)^o;c+=String.fromCharCode(l)}try{c=decodeURIComponent(escape(c))}catch(t){e(t)}return function(e){return i.innerHTML='<a href="'+e.replace(/"/g,"&quot;")+'"></a>',i.childNodes[0].getAttribute("href")||""}(c)}function n(t){try{(function(t){for(var n=t.querySelectorAll("a"),o=0;o<n.length;o++)try{var a=n[o],i=a.href.indexOf(c);i>-1&&(a.href="mailto:"+r(a.href,i+c.length))}catch(t){e(t)}})(t),function(t){for(var n=t.querySelectorAll(o),c=0;c<n.length;c++)try{var i=n[c],l=i.parentNode,u=i.getAttribute(a);if(u){var f=r(u,0),d=document.createTextNode(f);l.replaceChild(d,i)}}catch(t){e(t)}}(t),function(t){for(var r=t.querySelectorAll("template"),c=0;c<r.length;c++)try{n(r[c].content)}catch(t){e(t)}}(t)}catch(t){e(t)}}var c="/cdn-cgi/l/email-protection#",o=".__cf_email__",a="data-cfemail",i=document.createElement("div");n(document),function(){var e=document.currentScript||document.scripts[document.scripts.length-1];e.parentNode.removeChild(e)}()}();
  </script>
  <script>
   (function(){function c(){var b=a.contentDocument||a.contentWindow.document;if(b){var d=b.createElement('script');d.innerHTML="window.__CF$cv$params={r:'8ea97d990ab82021',t:'MTczMjk1NTU4NS4wMDAwMDA='};var a=document.createElement('script');a.nonce='';a.src='/cdn-cgi/challenge-platform/scripts/jsd/main.js';document.getElementsByTagName('head')[0].appendChild(a);";b.getElementsByTagName('head')[0].appendChild(d)}}if(document.body){var a=document.createElement('iframe');a.height=1;a.width=1;a.style.position='absolute';a.style.top=0;a.style.left=0;a.style.border='none';a.style.visibility='hidden';document.body.appendChild(a);if('loading'!==document.readyState)c();else if(window.addEventListener)document.addEventListener('DOMContentLoaded',c);else{var e=document.onreadystatechange||function(){};document.onreadystatechange=function(b){e(b);'loading'!==document.readyState&&(document.onreadystatechange=e,c())}}}})();
  </script>
 </body>
</html>

Using BeautifulSoup or the read_html function extract the table with Tesla Revenue and store it into a dataframe named tesla_revenue. The dataframe should have columns Date and Revenue.

Step-by-step instructions

Here are the step-by-step instructions:

1. Create an Empty DataFrame
2. Find the Relevant Table
3. Check for the Tesla Quarterly Revenue Table
4. Iterate Through Rows in the Table Body
5. Extract Data from Columns
6. Append Data to the DataFrame

Click here if you need help locating the table
    
Below is the code to isolate the table, you will now need to loop through the rows and columns like in the previous lab
    
soup.find_all("tbody")[1]
    
If you want to use the read_html function the table is located at index 1

We are focusing on quarterly revenue in the lab.
In [52]:
import pandas as pd
from bs4 import BeautifulSoup
import requests


url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm"
response = requests.get(url)
html_data = response.text


soup = BeautifulSoup(html_data, "html.parser")


rows = []


for table in soup.find_all('table'):
    
    if table.find('th') and table.find('th').getText().startswith("Tesla Quarterly Revenue"):
        
        for row in table.find("tbody").find_all("tr"):
            col = row.find_all("td")
           
            if len(col) != 2:
                continue
            
            Date = col[0].text.strip()
            Revenue = col[1].text.replace("$", "").replace(",", "").strip()
    
            rows.append({"Date": Date, "Revenue": Revenue})


tesla_revenue = pd.DataFrame(rows)


tesla_revenue["Revenue"] = pd.to_numeric(tesla_revenue["Revenue"], errors="coerce")


print(tesla_revenue)
          Date  Revenue
0   2022-09-30  21454.0
1   2022-06-30  16934.0
2   2022-03-31  18756.0
3   2021-12-31  17719.0
4   2021-09-30  13757.0
5   2021-06-30  11958.0
6   2021-03-31  10389.0
7   2020-12-31  10744.0
8   2020-09-30   8771.0
9   2020-06-30   6036.0
10  2020-03-31   5985.0
11  2019-12-31   7384.0
12  2019-09-30   6303.0
13  2019-06-30   6350.0
14  2019-03-31   4541.0
15  2018-12-31   7226.0
16  2018-09-30   6824.0
17  2018-06-30   4002.0
18  2018-03-31   3409.0
19  2017-12-31   3288.0
20  2017-09-30   2985.0
21  2017-06-30   2790.0
22  2017-03-31   2696.0
23  2016-12-31   2285.0
24  2016-09-30   2298.0
25  2016-06-30   1270.0
26  2016-03-31   1147.0
27  2015-12-31   1214.0
28  2015-09-30    937.0
29  2015-06-30    955.0
30  2015-03-31    940.0
31  2014-12-31    957.0
32  2014-09-30    852.0
33  2014-06-30    769.0
34  2014-03-31    621.0
35  2013-12-31    615.0
36  2013-09-30    431.0
37  2013-06-30    405.0
38  2013-03-31    562.0
39  2012-12-31    306.0
40  2012-09-30     50.0
41  2012-06-30     27.0
42  2012-03-31     30.0
43  2011-12-31     39.0
44  2011-09-30     58.0
45  2011-06-30     58.0
46  2011-03-31     49.0
47  2010-12-31     36.0
48  2010-09-30     31.0
49  2010-06-30     28.0
50  2010-03-31     21.0
51  2009-12-31      NaN
52  2009-09-30     46.0
53  2009-06-30     27.0

Execute the following line to remove the comma and dollar sign from the Revenue column.

In [ ]:
tesla_revenue["Revenue"] = tesla_revenue['Revenue'].str.replace(',|\$',"")

Execute the following lines to remove an null or empty strings in the Revenue column.

In [ ]:
tesla_revenue.dropna(inplace=True)

tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]

Display the last 5 row of the tesla_revenue dataframe using the tail function. Take a screenshot of the results.

In [27]:
print(tesla_revenue.tail(5))
          Date  Revenue
49  2010-06-30     28.0
50  2010-03-31     21.0
51  2009-12-31      NaN
52  2009-09-30     46.0
53  2009-06-30     27.0

Question 3: Use yfinance to Extract Stock Data¶

Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is GameStop and its ticker symbol is GME.

In [28]:
gme = yf.Ticker('GME')

Using the ticker object and the function history extract stock information and save it in a dataframe named gme_data. Set the period parameter to "max" so we get information for the maximum amount of time.

In [29]:
gme_data = gme.history(period = "max")

Reset the index using the reset_index(inplace=True) function on the gme_data DataFrame and display the first five rows of the gme_data dataframe using the head function. Take a screenshot of the results and code from the beginning of Question 3 to the results below.

In [30]:
gme_data.reset_index(inplace=True)
gme_data.head(5)
Out[30]:
Date Open High Low Close Volume Dividends Stock Splits
0 2002-02-13 00:00:00-05:00 1.620129 1.693350 1.603296 1.691667 76216000 0.0 0.0
1 2002-02-14 00:00:00-05:00 1.712708 1.716074 1.670626 1.683251 11021600 0.0 0.0
2 2002-02-15 00:00:00-05:00 1.683250 1.687458 1.658002 1.674834 8389600 0.0 0.0
3 2002-02-19 00:00:00-05:00 1.666418 1.666418 1.578047 1.607504 7410400 0.0 0.0
4 2002-02-20 00:00:00-05:00 1.615920 1.662210 1.603296 1.662210 6892800 0.0 0.0

Question 4: Use Webscraping to Extract GME Revenue Data¶

Use the requests library to download the webpage https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html. Save the text of the response as a variable named html_data_2.

In [ ]:
pip install requests beautifulsoup4 pandas
In [ ]:
url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html"
response = requests.get(url)
html_data_2 = response.text

Parse the html data using beautiful_soup using parser i.e html5lib or html.parser.

In [42]:
gme_soup = BeautifulSoup(html_data_2, 'html.parser')
In [47]:
gme_soup.find_all('tbody')
Out[47]:
[<tbody>
 <tr>
 <td style="text-align:center">2020</td>
 <td style="text-align:center">$6,466</td>
 </tr>
 <tr>
 <td style="text-align:center">2019</td>
 <td style="text-align:center">$8,285</td>
 </tr>
 <tr>
 <td style="text-align:center">2018</td>
 <td style="text-align:center">$8,547</td>
 </tr>
 <tr>
 <td style="text-align:center">2017</td>
 <td style="text-align:center">$7,965</td>
 </tr>
 <tr>
 <td style="text-align:center">2016</td>
 <td style="text-align:center">$9,364</td>
 </tr>
 <tr>
 <td style="text-align:center">2015</td>
 <td style="text-align:center">$9,296</td>
 </tr>
 <tr>
 <td style="text-align:center">2014</td>
 <td style="text-align:center">$9,040</td>
 </tr>
 <tr>
 <td style="text-align:center">2013</td>
 <td style="text-align:center">$8,887</td>
 </tr>
 <tr>
 <td style="text-align:center">2012</td>
 <td style="text-align:center">$9,551</td>
 </tr>
 <tr>
 <td style="text-align:center">2011</td>
 <td style="text-align:center">$9,474</td>
 </tr>
 <tr>
 <td style="text-align:center">2010</td>
 <td style="text-align:center">$9,078</td>
 </tr>
 <tr>
 <td style="text-align:center">2009</td>
 <td style="text-align:center">$8,806</td>
 </tr>
 <tr>
 <td style="text-align:center">2008</td>
 <td style="text-align:center">$7,094</td>
 </tr>
 <tr>
 <td style="text-align:center">2007</td>
 <td style="text-align:center">$5,319</td>
 </tr>
 <tr>
 <td style="text-align:center">2006</td>
 <td style="text-align:center">$3,092</td>
 </tr>
 <tr>
 <td style="text-align:center">2005</td>
 <td style="text-align:center">$1,843</td>
 </tr>
 </tbody>,
 <tbody>
 <tr>
 <td style="text-align:center">2020-04-30</td>
 <td style="text-align:center">$1,021</td>
 </tr>
 <tr>
 <td style="text-align:center">2020-01-31</td>
 <td style="text-align:center">$2,194</td>
 </tr>
 <tr>
 <td style="text-align:center">2019-10-31</td>
 <td style="text-align:center">$1,439</td>
 </tr>
 <tr>
 <td style="text-align:center">2019-07-31</td>
 <td style="text-align:center">$1,286</td>
 </tr>
 <tr>
 <td style="text-align:center">2019-04-30</td>
 <td style="text-align:center">$1,548</td>
 </tr>
 <tr>
 <td style="text-align:center">2019-01-31</td>
 <td style="text-align:center">$3,063</td>
 </tr>
 <tr>
 <td style="text-align:center">2018-10-31</td>
 <td style="text-align:center">$1,935</td>
 </tr>
 <tr>
 <td style="text-align:center">2018-07-31</td>
 <td style="text-align:center">$1,501</td>
 </tr>
 <tr>
 <td style="text-align:center">2018-04-30</td>
 <td style="text-align:center">$1,786</td>
 </tr>
 <tr>
 <td style="text-align:center">2018-01-31</td>
 <td style="text-align:center">$2,825</td>
 </tr>
 <tr>
 <td style="text-align:center">2017-10-31</td>
 <td style="text-align:center">$1,989</td>
 </tr>
 <tr>
 <td style="text-align:center">2017-07-31</td>
 <td style="text-align:center">$1,688</td>
 </tr>
 <tr>
 <td style="text-align:center">2017-04-30</td>
 <td style="text-align:center">$2,046</td>
 </tr>
 <tr>
 <td style="text-align:center">2017-01-31</td>
 <td style="text-align:center">$2,403</td>
 </tr>
 <tr>
 <td style="text-align:center">2016-10-31</td>
 <td style="text-align:center">$1,959</td>
 </tr>
 <tr>
 <td style="text-align:center">2016-07-31</td>
 <td style="text-align:center">$1,632</td>
 </tr>
 <tr>
 <td style="text-align:center">2016-04-30</td>
 <td style="text-align:center">$1,972</td>
 </tr>
 <tr>
 <td style="text-align:center">2016-01-31</td>
 <td style="text-align:center">$3,525</td>
 </tr>
 <tr>
 <td style="text-align:center">2015-10-31</td>
 <td style="text-align:center">$2,016</td>
 </tr>
 <tr>
 <td style="text-align:center">2015-07-31</td>
 <td style="text-align:center">$1,762</td>
 </tr>
 <tr>
 <td style="text-align:center">2015-04-30</td>
 <td style="text-align:center">$2,061</td>
 </tr>
 <tr>
 <td style="text-align:center">2015-01-31</td>
 <td style="text-align:center">$3,476</td>
 </tr>
 <tr>
 <td style="text-align:center">2014-10-31</td>
 <td style="text-align:center">$2,092</td>
 </tr>
 <tr>
 <td style="text-align:center">2014-07-31</td>
 <td style="text-align:center">$1,731</td>
 </tr>
 <tr>
 <td style="text-align:center">2014-04-30</td>
 <td style="text-align:center">$1,996</td>
 </tr>
 <tr>
 <td style="text-align:center">2014-01-31</td>
 <td style="text-align:center">$3,684</td>
 </tr>
 <tr>
 <td style="text-align:center">2013-10-31</td>
 <td style="text-align:center">$2,107</td>
 </tr>
 <tr>
 <td style="text-align:center">2013-07-31</td>
 <td style="text-align:center">$1,384</td>
 </tr>
 <tr>
 <td style="text-align:center">2013-04-30</td>
 <td style="text-align:center">$1,865</td>
 </tr>
 <tr>
 <td style="text-align:center">2013-01-31</td>
 <td style="text-align:center">$3,562</td>
 </tr>
 <tr>
 <td style="text-align:center">2012-10-31</td>
 <td style="text-align:center">$1,773</td>
 </tr>
 <tr>
 <td style="text-align:center">2012-07-31</td>
 <td style="text-align:center">$1,550</td>
 </tr>
 <tr>
 <td style="text-align:center">2012-04-30</td>
 <td style="text-align:center">$2,002</td>
 </tr>
 <tr>
 <td style="text-align:center">2012-01-31</td>
 <td style="text-align:center">$3,579</td>
 </tr>
 <tr>
 <td style="text-align:center">2011-10-31</td>
 <td style="text-align:center">$1,947</td>
 </tr>
 <tr>
 <td style="text-align:center">2011-07-31</td>
 <td style="text-align:center">$1,744</td>
 </tr>
 <tr>
 <td style="text-align:center">2011-04-30</td>
 <td style="text-align:center">$2,281</td>
 </tr>
 <tr>
 <td style="text-align:center">2011-01-31</td>
 <td style="text-align:center">$3,693</td>
 </tr>
 <tr>
 <td style="text-align:center">2010-10-31</td>
 <td style="text-align:center">$1,899</td>
 </tr>
 <tr>
 <td style="text-align:center">2010-07-31</td>
 <td style="text-align:center">$1,799</td>
 </tr>
 <tr>
 <td style="text-align:center">2010-04-30</td>
 <td style="text-align:center">$2,083</td>
 </tr>
 <tr>
 <td style="text-align:center">2010-01-31</td>
 <td style="text-align:center">$3,524</td>
 </tr>
 <tr>
 <td style="text-align:center">2009-10-31</td>
 <td style="text-align:center">$1,835</td>
 </tr>
 <tr>
 <td style="text-align:center">2009-07-31</td>
 <td style="text-align:center">$1,739</td>
 </tr>
 <tr>
 <td style="text-align:center">2009-04-30</td>
 <td style="text-align:center">$1,981</td>
 </tr>
 <tr>
 <td style="text-align:center">2009-01-31</td>
 <td style="text-align:center">$3,492</td>
 </tr>
 <tr>
 <td style="text-align:center">2008-10-31</td>
 <td style="text-align:center">$1,696</td>
 </tr>
 <tr>
 <td style="text-align:center">2008-07-31</td>
 <td style="text-align:center">$1,804</td>
 </tr>
 <tr>
 <td style="text-align:center">2008-04-30</td>
 <td style="text-align:center">$1,814</td>
 </tr>
 <tr>
 <td style="text-align:center">2008-01-31</td>
 <td style="text-align:center">$2,866</td>
 </tr>
 <tr>
 <td style="text-align:center">2007-10-31</td>
 <td style="text-align:center">$1,611</td>
 </tr>
 <tr>
 <td style="text-align:center">2007-07-31</td>
 <td style="text-align:center">$1,338</td>
 </tr>
 <tr>
 <td style="text-align:center">2007-04-30</td>
 <td style="text-align:center">$1,279</td>
 </tr>
 <tr>
 <td style="text-align:center">2007-01-31</td>
 <td style="text-align:center">$2,304</td>
 </tr>
 <tr>
 <td style="text-align:center">2006-10-31</td>
 <td style="text-align:center">$1,012</td>
 </tr>
 <tr>
 <td style="text-align:center">2006-07-31</td>
 <td style="text-align:center">$963</td>
 </tr>
 <tr>
 <td style="text-align:center">2006-04-30</td>
 <td style="text-align:center">$1,040</td>
 </tr>
 <tr>
 <td style="text-align:center">2006-01-31</td>
 <td style="text-align:center">$1,667</td>
 </tr>
 <tr>
 <td style="text-align:center">2005-10-31</td>
 <td style="text-align:center">$534</td>
 </tr>
 <tr>
 <td style="text-align:center">2005-07-31</td>
 <td style="text-align:center">$416</td>
 </tr>
 <tr>
 <td style="text-align:center">2005-04-30</td>
 <td style="text-align:center">$475</td>
 </tr>
 <tr>
 <td style="text-align:center">2005-01-31</td>
 <td style="text-align:center">$709</td>
 </tr>
 </tbody>,
 <tbody>
 <tr>
 <td style="text-align:center"><a href="https://web.archive.org/web/20200814131437/https://www.macrotrends.net/stocks/sector/3/retail-wholesale">Retail/Wholesale</a></td>
 <td style="text-align:center"><a href="https://web.archive.org/web/20200814131437/https://www.macrotrends.net/stocks/industry/156/">Retail - Consumer Electronics</a></td>
 <td style="text-align:center">$0.293B</td>
 <td style="text-align:center">$6.466B</td>
 </tr>
 <tr>
 <td colspan="4" style="padding:15px;">
 <span>GameStop Corp. is the world's largest video game and entertainment software retailer. The company operates 4,816 retail stores across the United States and in fifteen countries worldwide. The company also operates two e-commerce sites, GameStop.com and EBgames.com, and publishes Game Informer? magazine, a leading multi-platform video game publication. GameStop Corp. sells new and used video game software, hardware and accessories for next generation video game systems from Sony, Nintendo, and Microsoft. In addition, the company sells PC entertainment software, related accessories and other merchandise.</span>
 </td>
 </tr>
 </tbody>,
 <tbody>
 <tr>
 <td style="text-align:left"><a href="https://web.archive.org/web/20200814131437/https://www.macrotrends.net/stocks/charts/BBY/best-buy/revenue">Best Buy (BBY)</a></td>
 <td style="text-align:center">United States</td>
 <td style="text-align:center">$27.033B</td>
 <td style="text-align:center">18.16</td>
 </tr>
 <tr>
 <td style="text-align:left"><a href="https://web.archive.org/web/20200814131437/https://www.macrotrends.net/stocks/charts/AAN/aarons,-/revenue">Aaron's,  (AAN)</a></td>
 <td style="text-align:center">United States</td>
 <td style="text-align:center">$3.975B</td>
 <td style="text-align:center">15.14</td>
 </tr>
 <tr>
 <td style="text-align:left"><a href="https://web.archive.org/web/20200814131437/https://www.macrotrends.net/stocks/charts/GMELY/gome-retail-holdings/revenue">GOME Retail Holdings (GMELY)</a></td>
 <td style="text-align:center">China</td>
 <td style="text-align:center">$1.684B</td>
 <td style="text-align:center">0.00</td>
 </tr>
 <tr>
 <td style="text-align:left"><a href="https://web.archive.org/web/20200814131437/https://www.macrotrends.net/stocks/charts/SYX/systemax/revenue">Systemax (SYX)</a></td>
 <td style="text-align:center">United States</td>
 <td style="text-align:center">$0.873B</td>
 <td style="text-align:center">18.34</td>
 </tr>
 <tr>
 <td style="text-align:left"><a href="https://web.archive.org/web/20200814131437/https://www.macrotrends.net/stocks/charts/CONN/conns/revenue">Conn's (CONN)</a></td>
 <td style="text-align:center">United States</td>
 <td style="text-align:center">$0.325B</td>
 <td style="text-align:center">0.00</td>
 </tr>
 <tr>
 <td style="text-align:left"><a href="https://web.archive.org/web/20200814131437/https://www.macrotrends.net/stocks/charts/TAIT/taitron-components/revenue">Taitron Components (TAIT)</a></td>
 <td style="text-align:center">United States</td>
 <td style="text-align:center">$0.016B</td>
 <td style="text-align:center">10.50</td>
 </tr>
 </tbody>,
 <tbody>
 <tr>
 <td><a>GameStop Revenue 2006-2020 | GME</a></td>
 <td><input class="modal_link" size="60" type="text" value="&lt;a href='https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue'&gt;GameStop Revenue 2006-2020 | GME&lt;/a&gt;"/></td>
 </tr>
 <tr>
 <td><a>Macrotrends</a></td>
 <td><input class="modal_link" size="60" type="text" value="&lt;a href='https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue'&gt;Macrotrends&lt;/a&gt;"/></td>
 </tr>
 <tr>
 <td><a>Source</a></td>
 <td><input class="modal_link" size="60" type="text" value="&lt;a href='https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue'&gt;Source&lt;/a&gt;"/></td>
 </tr>
 </tbody>,
 <tbody>
 <tr>
 <td><a>GameStop Revenue 2006-2020 | GME</a></td>
 <td><input class="modal_link" size="50" type="text" value="&lt;a href='https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue'&gt;GameStop Revenue 2006-2020 | GME&lt;/a&gt;"/></td>
 </tr>
 <tr>
 <td><a>Macrotrends</a></td>
 <td><input class="modal_link" size="50" type="text" value="&lt;a href='https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue'&gt;Macrotrends&lt;/a&gt;"/></td>
 </tr>
 <tr>
 <td><a>Source</a></td>
 <td><input class="modal_link" size="50" type="text" value="&lt;a href='https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue'&gt;Source&lt;/a&gt;"/></td>
 </tr>
 </tbody>]

Using BeautifulSoup or the read_html function extract the table with GameStop Revenue and store it into a dataframe named gme_revenue. The dataframe should have columns Date and Revenue. Make sure the comma and dollar sign is removed from the Revenue column.

In [53]:
gme_QR = gme_soup.find_all('tbody')[1]

rows = []

for row in gme_QR.find_all("tr"):
    col = row.find_all("td")
    if col:  
        date = col[0].text.strip()  
        revenue = col[1].text.strip().replace(",", "").replace("$", "")  # Clean revenue
        rows.append({"Date": date, "Revenue": revenue})  

gme_revenue = pd.DataFrame(rows, columns=['Date', 'Revenue'])

gme_revenue['Revenue'] = pd.to_numeric(gme_revenue['Revenue'], errors='coerce')

gme_revenue
Out[53]:
Date Revenue
0 2020-04-30 1021
1 2020-01-31 2194
2 2019-10-31 1439
3 2019-07-31 1286
4 2019-04-30 1548
... ... ...
57 2006-01-31 1667
58 2005-10-31 534
59 2005-07-31 416
60 2005-04-30 475
61 2005-01-31 709

62 rows × 2 columns

Note: Use the method similar to what you did in question 2.

Click here if you need help locating the table
    
Below is the code to isolate the table, you will now need to loop through the rows and columns like in the previous lab
    
soup.find_all("tbody")[1]
    
If you want to use the read_html function the table is located at index 1


In [45]:
gme_revenue["Revenue"] = gme_revenue["Revenue"].astype(str)

gme_revenue["Revenue"] = gme_revenue["Revenue"].str.replace(r"[,|$]", "", regex=True)

gme_revenue.dropna(inplace=True)

gme_revenue = gme_revenue[gme_revenue["Revenue"] != ""]

gme_revenue["Revenue"] = pd.to_numeric(gme_revenue["Revenue"], errors="coerce")

gme_revenue.dropna(subset=["Revenue"], inplace=True)

print(gme_revenue.tail(5))
    Date  Revenue
11  2009     8806
12  2008     7094
13  2007     5319
14  2006     3092
15  2005     1843

Display the last five rows of the gme_revenue dataframe using the tail function. Take a screenshot of the results.

In [49]:
print(gme_revenue.tail(5))
          Date  Revenue
57  2006-01-31     1667
58  2005-10-31      534
59  2005-07-31      416
60  2005-04-30      475
61  2005-01-31      709

Question 5: Plot Tesla Stock Graph¶

Use the make_graph function to graph the Tesla Stock Data, also provide a title for the graph. Note the graph will only show data upto June 2021.

Hint

You just need to invoke the make_graph function with the required parameter to print the graphs.The structure to call the `make_graph` function is `make_graph(tesla_data, tesla_revenue, 'Tesla')`.

In [50]:
make_graph(tesla_data, tesla_revenue, 'Tesla')
/tmp/ipykernel_1157/3316612210.py:5: UserWarning:

The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.

/tmp/ipykernel_1157/3316612210.py:6: UserWarning:

The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.

Question 6: Plot GameStop Stock Graph¶

Use the make_graph function to graph the GameStop Stock Data, also provide a title for the graph. The structure to call the make_graph function is make_graph(gme_data, gme_revenue, 'GameStop'). Note the graph will only show data upto June 2021.

Hint

You just need to invoke the make_graph function with the required parameter to print the graphs.The structure to call the `make_graph` function is `make_graph(gme_data, gme_revenue, 'GameStop')`

In [51]:
make_graph(gme_data, gme_revenue, 'GameStop')
/tmp/ipykernel_1157/3316612210.py:5: UserWarning:

The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.

/tmp/ipykernel_1157/3316612210.py:6: UserWarning:

The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.

About the Authors:

Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.

Azim Hirjani

Change Log¶

Date (YYYY-MM-DD) Version Changed By Change Description
2022-02-28 1.2 Lakshmi Holla Changed the URL of GameStop
2020-11-10 1.1 Malika Singla Deleted the Optional part
2020-08-27 1.0 Malika Singla Added lab to GitLab

© IBM Corporation 2020. All rights reserved.